package OSMS.action.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.CustomerDao;
import OSMS.entity.Customers;

public class Cus_Update_Pwd_Servlet extends HttpServlet {

	
	CustomerDao cdao = new CustomerDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		Object obj_cus = session.getAttribute("cus");
		Customers cus = (Customers)obj_cus;
		int cid = cus.getCid();
		
		String pw_old = request.getParameter("pw_old");
		String pw_new = request.getParameter("pw_new");
		String pw_again = request.getParameter("pw_again");
		System.out.println(pw_old);
		System.out.println(pw_new);
		System.out.println(pw_again);
		if(!pw_old.equals(cus.getCpwd()))
		{
			request.setAttribute("fail_code", 1);
			request.getRequestDispatcher("../user/fail_to_update_pwd.jsp").forward(request, response);
			return ;
		}
		else if(!pw_new.equals(pw_again))
		{
			request.setAttribute("fail_code", 2);
			request.getRequestDispatcher("../user/fail_to_update_pwd.jsp").forward(request, response);
			return ;
		}
		else
		{
			boolean flag_udt_pwd = false;
			try {
				flag_udt_pwd = cdao.upDateCusPwd(pw_new,cid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.getRequestDispatcher("../user/successful_to_update_pwd.jsp").forward(request, response);
			
		}
				
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
}
